package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.JTZhengXiang;

import com.by.zhdoctor.repository.JTZhengXiangRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing JTZhengXiang.
 */
@RestController
@RequestMapping("/api")
public class JTZhengXiangResource {

    private final Logger log = LoggerFactory.getLogger(JTZhengXiangResource.class);

    @Inject
    private JTZhengXiangRepository jTZhengXiangRepository;

    /**
     * POST  /j-t-zheng-xiangs : Create a new jTZhengXiang.
     *
     * @param jTZhengXiang the jTZhengXiang to create
     * @return the ResponseEntity with status 201 (Created) and with body the new jTZhengXiang, or with status 400 (Bad Request) if the jTZhengXiang has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/j-t-zheng-xiangs")
    @Timed
    public ResponseEntity<JTZhengXiang> createJTZhengXiang(@RequestBody JTZhengXiang jTZhengXiang) throws URISyntaxException {
        log.debug("REST request to save JTZhengXiang : {}", jTZhengXiang);
        if (jTZhengXiang.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("jTZhengXiang", "idexists", "A new jTZhengXiang cannot already have an ID")).body(null);
        }
        JTZhengXiang result = jTZhengXiangRepository.save(jTZhengXiang);
        return ResponseEntity.created(new URI("/api/j-t-zheng-xiangs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("jTZhengXiang", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /j-t-zheng-xiangs : Updates an existing jTZhengXiang.
     *
     * @param jTZhengXiang the jTZhengXiang to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated jTZhengXiang,
     * or with status 400 (Bad Request) if the jTZhengXiang is not valid,
     * or with status 500 (Internal Server Error) if the jTZhengXiang couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/j-t-zheng-xiangs")
    @Timed
    public ResponseEntity<JTZhengXiang> updateJTZhengXiang(@RequestBody JTZhengXiang jTZhengXiang) throws URISyntaxException {
        log.debug("REST request to update JTZhengXiang : {}", jTZhengXiang);
        if (jTZhengXiang.getId() == null) {
            return createJTZhengXiang(jTZhengXiang);
        }
        JTZhengXiang result = jTZhengXiangRepository.save(jTZhengXiang);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("jTZhengXiang", jTZhengXiang.getId().toString()))
            .body(result);
    }

    /**
     * GET  /j-t-zheng-xiangs : get all the jTZhengXiangs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of jTZhengXiangs in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/j-t-zheng-xiangs")
    @Timed
    public ResponseEntity<List<JTZhengXiang>> getAllJTZhengXiangs(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of JTZhengXiangs");
        Page<JTZhengXiang> page = jTZhengXiangRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/j-t-zheng-xiangs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /j-t-zheng-xiangs/:id : get the "id" jTZhengXiang.
     *
     * @param id the id of the jTZhengXiang to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the jTZhengXiang, or with status 404 (Not Found)
     */
    @GetMapping("/j-t-zheng-xiangs/{id}")
    @Timed
    public ResponseEntity<JTZhengXiang> getJTZhengXiang(@PathVariable Long id) {
        log.debug("REST request to get JTZhengXiang : {}", id);
        JTZhengXiang jTZhengXiang = jTZhengXiangRepository.findOne(id);
        return Optional.ofNullable(jTZhengXiang)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /j-t-zheng-xiangs/:id : delete the "id" jTZhengXiang.
     *
     * @param id the id of the jTZhengXiang to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/j-t-zheng-xiangs/{id}")
    @Timed
    public ResponseEntity<Void> deleteJTZhengXiang(@PathVariable Long id) {
        log.debug("REST request to delete JTZhengXiang : {}", id);
        jTZhengXiangRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("jTZhengXiang", id.toString())).build();
    }



    /**
     * GET  /zheng-xings/:bmName : get the "id" bmName.
     *
     * @param zxmName the bmName of the zhengXing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @GetMapping("/j-t-zheng-xiangs-byZXM/{zxmName}/{bmName}")
    @Timed
    public ResponseEntity<List<JTZhengXiang>> getJTZXByBMName(@PathVariable String zxmName,@PathVariable String bmName) {
        log.debug("REST request to get ZhengXing : {}", zxmName);
        List<JTZhengXiang> jTZhengXiang = jTZhengXiangRepository.findAllByZhengxiangmingZxmNameAndZhengxiangmingBingmingBmName(zxmName,bmName);
        return Optional.ofNullable(jTZhengXiang)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

}
